package smartcase.config.log;

import org.springframework.stereotype.Component;
import smartcase.base.log.LogStruct;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class LogStructConsumer {

  private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

  private final CLSClient client;

  public LogStructConsumer() {
    this.client = new CLSClient();
  }

  public void reportLog2Cls(LogStruct log) {
    smartcase.config.log.CLS.LogGroupList logGroupList =
        smartcase.config.log.CLS.LogGroupList.newBuilder()
            .addLogGroupList(
                smartcase.config.log.CLS.LogGroup.newBuilder()
                    .addLogs(
                        smartcase.config.log.CLS.Log.newBuilder()
                            .setTime(log.getTimestamp())
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("clientIP")
                                    .setValue(log.getClientIP()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("requestId")
                                    .setValue(log.getRequestId()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("path")
                                    .setValue(log.getPath()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("level")
                                    .setValue(log.getLevel()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("serverIP")
                                    .setValue(log.getServerIP()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("message")
                                    .setValue(log.getMessage()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("timestamp")
                                    .setValue(df.format(new Date(log.getTimestamp()))))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("className")
                                    .setValue(log.getClassName()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("lineNum")
                                    .setValue(log.getLineNum()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("fileName")
                                    .setValue(log.getFileName()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("method")
                                    .setValue(log.getMethod()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("method")
                                    .setValue(log.getEndpoint()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("module")
                                    .setValue(log.getModule()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("threadName")
                                    .setValue(log.getThreadName()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("exceptionMsg")
                                    .setValue(log.getExceptionMsg()))
                            .addContents(
                                smartcase.config.log.CLS.Log.Content.newBuilder()
                                    .setKey("extInfo")
                                    .setValue(log.getExtInfo()))))
            .build();

    try {
      PostStructLogRequest req;
      if ((req = client.newPostStructLogRequest(logGroupList)) != null) {
        String result = client.send(req);
        System.err.println(result);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
